local nkLibQuests = LibStub("nkLibQuests");

nkPartyQuest.Quests = {};
nkPartyQuest.Quests.SnapShot = {};
nkPartyQuest_CompletedQuests = {};

-- Builds and returns a sorted list of quests (table)
-- Input: QuestDetails table - structure according nkLibQuests.GetQuestList
-- Output: Sorted table with elements .name and .ident; 
nkPartyQuest.Quests.GetSortedQuestList = function (QuestDetails)

	local SortedList = {};
	
	--Sol.io.Printf (tostring(#QuestDetails));
	
	for k in pairs(QuestDetails) do
		--Sol.io.Printf (QuestDetails[idx].name);
		table.insert (SortedList, {name = QuestDetails[k].name; ident= k});		
	end;
	
	table.sort (SortedList, function (a,b) return (a.name < b.name) end);
	--table.sort (SortedList);
	return SortedList;

end;

-- Gets the details of a specific quest from the global table
-- Input:	questname	- The name of the quest
-- Output:	Quest table structure
nkPartyQuest.Quests.GetQuestDetailsByName = function (questname)

	--DEFAULT_CHAT_FRAME:AddMessage("looking for " ..questname);

	if nkPartyQuest.Core.QuestList[UnitName("player")] == nil then 
		return nil;
	end;

	for k in pairs(nkPartyQuest.Core.QuestList[UnitName("player")]) do
		--DEFAULT_CHAT_FRAME:AddMessage(nkPartyQuest.Core.OwnQuestList[k].name);
		if nkPartyQuest.Core.QuestList[UnitName("player")][k].name == questname then
			--DEFAULT_CHAT_FRAME:AddMessage("found it");
			return nkPartyQuest.Core.QuestList[UnitName("player")] [k];
		end;
	end;
	
	return nil;
end;

nkPartyQuest.Quests.GetQuestUpdates  = function ()

	local tmpUpdates = {};
	nkPartyQuest.Core.QuestList[UnitName("player")] = nkLibQuests.GetQuestList();
	
	if nkPartyQuest.Quests.Snapshot == nil then
		-- No snapshot available return full list
		--DEFAULT_CHAT_FRAME:AddMessage("no snapshot -> send full list");
		nkPartyQuest.Quests.Snapshot = nkPartyQuest.Core.QuestList[UnitName("player")];
		return nkPartyQuest.Core.QuestList[UnitName("player")];
	elseif nkPartyQuest.Core.QuestList[UnitName("player")] == nil then
		-- No quests active, prepare quest deletion update
		--DEFAULT_CHAT_FRAME:AddMessage("no quest active");
		for k in pairs (nkPartyQuest.Quests.Snapshot) do
			nkPartyQuest.Quests.Snapshot[k].status = 0;			
		end;
		
		tmpUpdates = nkPartyQuest.Quests.Snapshot;
		nkPartyQuest.Quests.Snapshot = nil;
		return tmpUpdates;
	end;
	
	for k in pairs (nkPartyQuest.Quests.Snapshot) do
		if nkPartyQuest.Core.QuestList[UnitName("player")][k] == nil then
			-- Quest no longer in list -> aborted or completed at NPC
			--DEFAULT_CHAT_FRAME:AddMessage("aborted or completed quest " .. k);
			nkPartyQuest.Quests.Snapshot[k].status = 0;
			tmpUpdates[k] = nkPartyQuest.Quests.Snapshot[k];
		elseif nkPartyQuest.Core.QuestList[UnitName("player")][k].status ~= nkPartyQuest.Quests.Snapshot[k].status then
			-- Completion status changed
			--DEFAULT_CHAT_FRAME:AddMessage("completion status changed " .. k);
			if nkPartyQuest.Core.QuestList[UnitName("player")][k].status == 9 then
				if UnitExists("party1") then SendChatMessage(nkPartyQuest.Lang.Common.QUESTFINISHED .. " " .. nkPartyQuest.Core.QuestList[UnitName("player")][k].name, "PARTY" ); end;
				nkPartyQuest_CompletedQuests[k] = nkPartyQuest.Core.QuestList[UnitName("player")][k].name;			
				SaveVariablesPerCharacter("nkPartyQuest_CompletedQuests");
			end;
			
			tmpUpdates[k] = nkPartyQuest.Core.QuestList[UnitName("player")][k];
		end;
	end;
	
	for k in pairs (nkPartyQuest.Core.QuestList[UnitName("player")]) do
		if nkPartyQuest.Quests.Snapshot[k] == nil then
			-- New quest found
			--DEFAULT_CHAT_FRAME:AddMessage("new quest " .. k);
			tmpUpdates[k] = nkPartyQuest.Core.QuestList[UnitName("player")][k];
		end;
	end;
	
	nkPartyQuest.Quests.Snapshot = nkPartyQuest.Core.QuestList[UnitName("player")];
	
	return tmpUpdates;

end;

nkPartyQuest.Quests.GetNewQuestName = function ()
	
	local tmpQuestList = nkLibQuests.GetQuestList();
			
	for k in pairs(tmpQuestList) do
		if nkPartyQuest.Core.QuestList[UnitName("player")] == nil then
			return tmpQuestList[k].name;
		elseif (nkPartyQuest.Core.QuestList[UnitName("player")][k] == nil) then
			return tmpQuestList[k].name;
		end;
	end;
	
	return nil;
	
end;

nkPartyQuest.Quests.GetCatalogList = function ()

	local catalogList = {};

--	if nkPartyQuest_CompletedQuests ~= nil and nkLibQuests_QBase ~= nil then
--		for k in pairs(nkPartyQuest_CompletedQuests) do
--			if nkLibQuests_QBase[k] ~= nil then
--				local catalogName = GetQuestCatalogInfo(nkLibQuests_QBase.catalogID);
--				if Sol.table.Contains (catalogList, catalogName) == false then
--					table.insert (catlogList, catalogName);
--				end;
--			end;
--		end;
--	end;

	if nkLibQuests_QBase ~= nil then
		for k in pairs(nkLibQuests_QBase) do
			local catalogName = GetQuestCatalogInfo(nkLibQuests_QBase[k].catalogID);
			if Sol.table.Contains (catalogList, catalogName) == false then
				table.insert (catalogList, catalogName);
			end;
		end;
	end;
	
	if nkPartyQuest.Core.QuestList[UnitName("player")] ~= nil then
		for k in pairs(nkPartyQuest.Core.QuestList[UnitName("player")]) do
			local catalogName = GetQuestCatalogInfo(nkPartyQuest.Core.QuestList[UnitName("player")][k].catalogID);
			if Sol.table.Contains (catalogList, catalogName) == false then
				table.insert (catalogList, catalogName);
			end;			
		end;
	end;	
	
	return catalogList;
	
end;

nkPartyQuest.Quests.GetCatalogIDFromName = function (catalogName)

	for k in pairs(nkLibQuests_QBase) do
		local tmpcatalogName = GetQuestCatalogInfo(nkLibQuests_QBase[k].catalogID);
		if tmpcatalogName == catalogName then
			return nkLibQuests_QBase[k].catalogID;
		end;
	end;
	
	if nkPartyQuest.Core.QuestList[UnitName("player")] ~= nil then
		for k in pairs(nkPartyQuest.Core.QuestList[UnitName("player")]) do
			local tmpcatalogName = GetQuestCatalogInfo(nkPartyQuest.Core.QuestList[UnitName("player")][k].catalogID);
			if tmpcatalogName == catalogName then
				return nkPartyQuest.Core.QuestList[UnitName("player")][k].catalogID;
			end;
		end;
	end;
	
	return nil;
	
end;

nkPartyQuest.Quests.GetHistoryQuests = function (catalogName)
	
	local questlist = {};
	
	local catalogID = nkPartyQuest.Quests.GetCatalogIDFromName(catalogName);	
	if catalogID == nil then return nil; end;
	
	for k in pairs(nkLibQuests_QBase) do
		if nkLibQuests_QBase[k].catalogID == catalogID then
			table.insert (questlist,nkLibQuests_QBase[k]);
		end; 
	end;
	
	if nkPartyQuest.Core.QuestList[UnitName("player")] ~= nil then
		for k in pairs(nkPartyQuest.Core.QuestList[UnitName("player")]) do
			if nkPartyQuest.Core.QuestList[UnitName("player")][k].catalogID == catalogID then
			
				local isInTable = false;
			
				for idx = 1, #questlist do
					if questlist[idx].name == nkPartyQuest.Core.QuestList[UnitName("player")][k].name then
						isInTable = true;
						break;
					end;
				end;
			
				if isInTable == false then
					table.insert (questlist, nkPartyQuest.Core.QuestList[UnitName("player")][k]);
				end;
			end; 
		end;
	end;

	return questlist;	
	
end;